package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.PolularScienceResourcesDao;
import com.hp.entity.PolularScienceResources;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.PolularScienceResourcesService;

import com.hp.utils.UUIDBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class PolularScienceResourcesServiceImpl implements PolularScienceResourcesService {

    @Autowired
    private PolularScienceResourcesDao polularScienceResourcesDao;
    @Autowired
    private PolularScienceResourcesDao typePolularScienceResourcesDao;

    @Override
    public SubmitResultInfo saveresource(CommonsMultipartFile file, PolularScienceResources polularScienceResources, HttpServletRequest request) throws Exception {
        if (polularScienceResources == null) {
            ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 211,
                    null));
        }

        // 先处理上传的图片
        if (file != null && !file.isEmpty()) {
            String realPath = request.getSession().getServletContext()
                    .getRealPath("//upload//");
//            String realPath=Config.SaveUrl+ "//upload//";
            String typename = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1);
            String name = UUIDBuild.getUUID() + "." + typename;

            File f = new File(realPath + "//" + name);
            file.transferTo(f);
            polularScienceResources
                    .setResourceurl(Config.BaseUrl + "upload/" + name);
            polularScienceResources.setResource_type(3);
        } else {
            polularScienceResources.setResource_type(1);
        }

        // 保存资源
        polularScienceResourcesDao.saveresource(polularScienceResources);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 661, new Object[]{"类型"}));
    }

    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo getAllAndIsN(int page, int rows, PolularScienceResources polularScienceResources) throws Exception {
        Page<PolularScienceResources> startPage = PageHelper.startPage(page,
                rows);
        List<PolularScienceResources> pp = polularScienceResourcesDao.getAllAndIsN(polularScienceResources);
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());
        dataGridResultInfo.setTotal((int) startPage.getTotal());
        return dataGridResultInfo;
    }

    @Transactional(readOnly = true)
    @Override
    public PolularScienceResources findByid(int id) throws Exception {
        return polularScienceResourcesDao.findById(id + "");
    }

    @Override
    public SubmitResultInfo updateresource(CommonsMultipartFile file, PolularScienceResources polularScienceResources, HttpServletRequest request) throws Exception {
        if (polularScienceResources == null) {
            ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 211,
                    null));
        }
        // 先处理上传的图片
        if (file != null && !file.isEmpty()) {
            String realPath = request.getSession().getServletContext()
                    .getRealPath("/upload/");
            String typename = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1);
            String name = UUIDBuild.getUUID() + "." + typename;

            File f = new File(realPath + "\\" + name);
            file.transferTo(f);
            polularScienceResources
                    .setResourceurl(Config.BaseUrl + "upload\\" + name);
        }

        // 保存资源

        polularScienceResourcesDao.updateresource(polularScienceResources);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 666, null));


    }


    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public SubmitResultInfo deleteIsNpsr(int id) throws Exception {
        polularScienceResourcesDao.deleteById(id);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(
                Config.MESSAGE, 662, null));
    }

    /**
     * 更具id查询资源
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public PolularScienceResources findByid(String id) throws Exception {
        return polularScienceResourcesDao.findById(id);
    }

    @Override
    public SubmitResultInfo adoptpsr(PolularScienceResources polularScienceResources) throws Exception {
        polularScienceResourcesDao.updateStatus(polularScienceResources);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 666, new Object[] {"状态"}));
    }

    @Override
    public PolularScienceResources xiugai(int id) throws Exception {
        return polularScienceResourcesDao.xiugai(id);
    }


}